<?php


class AdminRole_UserManager extends UserManager {



	public function __construct() {
		$this->registry = Yapary_SingletonRegistry::getInstance();
		return;
	}


	public function addUser(User $_user, $_sameAdress) {

		if($_user == null || !isset($_user)) {
			return Error_ErrnoUser::$ERRNO_OBJECT_NULL;
		}

		/* @var $oUser User */
		$oUser = $_user;
		/* @var $oAddress Address */
		$oAddressDelivery = $oUser->getDeliveryAddress();
		if($oAddressDelivery == null) {
			return Error_ErrnoUser::$ERRNO_OBJECT_NULL;
		}
		/* @var $oAddress Address */
		$oAddressBill = $oUser->getBillAddress();
		/* @var $oRole Role */
		$oRole = $oUser->getRole();
		/* @var $oSql Yapary_Db_Sql_Connector */
		$oSql =  $this->registry->oSql;



		$oSql->request('INSERT INTO Adresse (Ort, Strasse, Plz, LaenderCode, TelefonNr)
				VALUES ("' . $oAddressDelivery->getCity() .  '",
				"' . $oAddressDelivery->getStreet() .  '",
				"' . $oAddressDelivery->getPlz() .  '",
				"' . $oAddressDelivery->getCountryCode() .  '",
				"' . $oAddressDelivery->getTelephone() .  '") ');

		$deliveryId = mysql_insert_id();
		if($_sameAdress != true) {

			if($oAddressBill == null) {
				return Error_ErrnoUser::$ERRNO_OBJECT_NULL;
			}
			
			$oSql->request('INSERT INTO Adresse (Ort, Strasse, Plz, LaenderCode, TelefonNr)
					VALUES ("' . $oAddressBill->getCity() .  '",
					"' . $oAddressBill->getStreet() .  '",
					"' . $oAddressBill->getPlz() .  '",
					"' . $oAddressBill->getCountryCode() .  '",
					"' . $oAddressBill->getTelephone() .  '") ');

			$billId = mysql_insert_id();
		}
		else {
			$billId =$deliveryId;
		}
		$oSql->request('INSERT INTO User (Passwort,Email,Newsletter,Status,Vorname,Nachname,besitztRolle,hatLieferAdresse,hatRechnungsAdresse)
				VALUES ("' . $oUser->getPassword() . '",
				"' . $oUser->getEmail() .  '",
				' . $oUser->getNewsletter() .  ',
				"' . $oUser->getStatus() .  '",
				"' . $oUser->getFirstname() .  '",
				"' . $oUser->getLastname() .  '",
				' . $oRole->getRoleId() .  ',
				' . $deliveryId .  ',
				' . $billId . ')');

		$user_id = mysql_insert_id();
		if($user_id != null && isset($user_id)) {
			return $user_id;
		}
		return Error_ErrnoUser::$ERRNO_USER_NOT_CREATED;
	}



	public function deleteUser(User $_user) {



		return;
	}


	public function getAll($_flag, $_sort, $_desc=false) {

		
		$filter = array_unique($_flag);
		$sort = array_unique($_sort);
		/* @var $oSql Yapary_Db_Sql_Connector */
		$oSql =  $this->registry->oSql;


		$query = 'SELECT * FROM user AS u';
		$filterQuery = '';
		$sortQuery = '';

		
		$count = count($filter);
		
		
		$first = true;
		$inserted = false;
		foreach ($filter as $a){
			if($first) {
				$filterQuery .= ' WHERE';
			}


			switch ($a){
				case Enum_EnumUserFilter::$FILTER_ROLE_ADMIN:
					($first) ? ($filterQuery .= ' AND u.besitztRolle = ' . Enum_EnumUserFilter::$FILTER_ROLE_ADMIN) :
											($filterQuery .= ' u.besitztRolle = ' . Enum_EnumUserFilter::$FILTER_ROLE_ADMIN); $count--; $first=false; $inserted=true; break;
				case Enum_EnumUserFilter::$FILTER_ROLE_USER:
					($first) ? ($filterQuery .= ' AND u.besitztRolle =' . Enum_EnumUserFilter::$FILTER_ROLE_USER) :
											($filterQuery .= ' u.besitztRolle = ' . Enum_EnumUserFilter::$FILTER_ROLE_USER); $count--; $first=false; $inserted=true; break;
				case Enum_EnumUserFilter::$FILTER_ROLE_INTERNAL:
					($first) ? ($filterQuery .= ' AND u.besitztRolle = ' . Enum_EnumUserFilter::$FILTER_ROLE_INTERNAL) :
											($filterQuery .= ' u.besitztRolle = ' . Enum_EnumUserFilter::$FILTER_ROLE_INTERNAL); $count--; $first=false; $inserted=true; break;
				default:
					$first = false;
			}
		}
//Zwei schleifen, da sonst das array in einer bestimmten reihenfolge befuellt werden muss
		foreach ($filter as $a){
			if($count <= 0) {
				break;
			}
			if($inserted) {
				$filterQuery .= ' AND u.status = ';
			}
			else {
				$filterQuery .= ' u.status = ';
			}

			switch ($a){
					case Enum_EnumUserFilter::$FILTER_STATUS_ACTIV:
						$filterQuery .= '"active"'; $count--; break;
					case Enum_EnumUserFilter::$FILTER_STATUS_INACTIV:
						$filterQuery .= '"inactive"'; $count--; break;
					default:
						continue 2;
			}
			break;
		}


		$first = true;
		foreach ($sort as $a){
			if($first) {
				$sortQuery .= ' ORDER BY ';
			}

			switch ($a){
				case Enum_EnumUserSort::$BY_FIRSTNAME:
					(!$first) ? ($sortQuery .= ', u.Vorname') :
										($sortQuery .= 'u.Vorname'); $first=false; break;
				case Enum_EnumUserSort::$BY_LASTNAME:
					(!$first) ? ($sortQuery .= ', u.Nachname') :
										($sortQuery .= 'u.Nachname'); $first=false; break;
				case Enum_EnumUserSort::$BY_ID:
					(!$first) ? ($sortQuery .= ', u.UserId') :
										($sortQuery .= 'u.UserId'); $first=false; break;
			}
		}

		$query .= $filterQuery . $sortQuery;

		if($_desc == true) {
			$query .= ' DESC;';
		}
		else {
			$query .= ' ASC;';
		}

		$result = $oSql->request($query);

		if(isset($result) && $result != null) {
			return $result;
		}

		return Error_ErrnoUser::$ERRNO_USERS_NOT_FOUND_IN_DB;
	}



}


?>
